package cn.autumnorange.app.user.api;

import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.user.api.dto.UserDetailDto;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SysUserFallbackFactory implements FallbackFactory<AppUserService> {

  @Override
  public AppUserService create(Throwable throwable) {
    return new AppUserService() {
      @Override
      public ResponseEntity<ResultDto<UserDetailDto>> loadUserByUsername(String name) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
            .body(ResultDto.createMessageResult(throwable.getMessage()));
      }

      @Override
      public ResponseEntity<ResultDto<UserDetailDto>> loginSuccess() {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
            .body(
                ResultDto.createResult(
                    HttpStatus.INTERNAL_SERVER_ERROR.value(),
                    null,
                    throwable.getMessage(),
                    null,
                    null,
                    HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()));
      }
    };
  }
}
